package com.newgrand.annexcommon.utils;

import java.util.Date;
import java.util.Random;

/**
 * @author ：minyang
 * @date ：Created in 2019/9/7 17:58
 */
public class NoUtil {

    /**
     * 获取指定长度的随机大小写英文数字组合
     *
     * @param length
     * @return
     */
    public static String getStringRandom(int length) {

        String val = "";
        Random random = new Random();

        //参数length，表示生成几位随机数
        while (true) {
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            //输出字母还是数字
            if ("char".equalsIgnoreCase(charOrNum)) {
                //输出是大写字母还是小写字母
                int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
                char c = (char) (random.nextInt(26) + temp);
                if (c == 'i' || c == 'o' || c == 'l') {
                    continue;
                }
                val += c;
            } else if ("num".equalsIgnoreCase(charOrNum)) {
                int n = random.nextInt(10);
                if (n == 0 || n == 1) {
                    continue;
                }
                val += String.valueOf(n);
            }
            if (val.length() == length) {
                break;
            }
        }
        return val;
    }

    public static String getNumberRandom(int len) {
        Random r = new Random();
        int bitNum = 1;
        for (int i = 0; i < len; i++) {
            bitNum = bitNum * 10;
        }
        long num = Math.abs(r.nextLong() % (bitNum));
        String s = String.valueOf(num);
        for (int i = len - s.length(); i > 0; i--) {
            s = "0" + s;
        }
        if (s.length() > len) {
            s = s.substring(0, len);
        }
        return s;
    }

    /**
     * 随机存储策略
     *
     * @return
     */
    public static String getDbToken() {
        //00yyyymmddHHmmss0001
        java.text.SimpleDateFormat df2 = new java.text.SimpleDateFormat("yyyyMMddHHmmss");
        Date now = new Date();
        String oid = getStringRandom(4);
        return "DB" + df2.format(now) + oid;
    }

    public static String getAsrFid() {
        //00yyyymmddHHmmss0001
        java.text.SimpleDateFormat df2 = new java.text.SimpleDateFormat("yyyyMMddHHmmss");
        Date now = new Date();
        String oid = getStringRandom(10);
        return "File" + df2.format(now) + oid;
    }


}
